package com.situ.controller;

import com.situ.bean.PageResult;
import com.situ.bean.Product;
import com.situ.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProductController {
    @Autowired
    IProductService productService;
    @RequestMapping("product")

    public String product(@RequestParam("id")int id, HttpSession session) {
        Product   product = productService.getProductById(id);
        System.out.println(product);
        System.out.println("session");
        session.setAttribute("product",product);

      return "redirect:product-two-details.html";
    }



    @RequestMapping("/products")
    public String getProducts(
            @RequestParam(defaultValue = "1") int page, // 默认第一页
            @RequestParam(defaultValue = "15") int size, // 默认每页 10 条
            Model model) {
       System.out.println("分页状况");
        // 获取分页数据
        PageResult<Product> pageResult = productService.getProducts(page, size);
        // 将数据传递给前端
        System.out.println(pageResult.getData().toString());
        model.addAttribute("products", pageResult.getData()); // 当前页的数据
        model.addAttribute("currentPage", page); // 当前页码
        model.addAttribute("totalPages", pageResult.getTotalPages()); // 总页数
        model.addAttribute("totalProducts", pageResult.getTotalItems()); // 总数据量

        return "product-category"; // 返回 Thymeleaf 模板名称
    }


}
